<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class File_model extends MY_CI_Model
{

    /**
     * @access public
     */
    public function __construct()
    {
        $this->table = 'sys_file_tmps';
        $this->primary_id = 'file_id';
        $this->primary_name = 'module';

        parent::__construct();

    }

    /**
     * 查询条件
     * @param array $filter
     * @return void
     */
    function list_query_filter($filter = array())
    {
        if (isset($filter['keywords']) && !empty($filter['keywords'])) {
            $whereKW = '(a.' . $this->primary_name . ' like "%' . $filter['keywords'] . '%" ' .
                'or a.' . $this->primary_id . ' like "%' . $filter['keywords'] . '%")';
            $this->db->where($whereKW);
        }
    }

    /**
     * 格式化输出字段
     * @param unknown $item_list
     * @return multitype:unknown
     */
    function reformat_list_datas($item_list)
    {
        $arr = array();
        foreach ($item_list as $row) {
            $row['format_thumb'] = get_image_path($row['thumb']);
            $row['format_image'] = get_image_path($row['image']);
            $row['format_original'] = get_image_path($row['original']);
            $arr[] = $row;
        }
        return $arr;
        return $item_list;
    }

    public function process_image_tp3($action, $original_url, $primary_id = 0)
    {
        $source_image = full_static_path($original_url);
        $source_size = getimagesize($source_image);

        $this->load->library('image');
        //生成大图
        if ($source_size[0] > 640) {
            $dest_width = 640;
            $dest_height = 380;
            if ($action == 'canteen_image') {
                $dest_height = 420;
            }
            $image_url = $this->reformat_image_name($action, 'Image', $original_url);
            $image_url_path = full_static_path($image_url);
            $image_url_handle = $this->image->make_thumb($source_image, $image_url_path, $dest_width, $dest_height);
            if ($image_url_handle === FALSE) {
                $this->result['error'] = 1;
                $this->result['message'] = $this->image->error_msg();
                return $this->result;
            }
        } else {
            $image_url = $original_url;
        }
        //缩略图路径
        $thumb_width = 320;
        $thumb_height = 200;
        if ($action == 'canteen_image') {
            $thumb_height = 260;
        }
        $thumb_url = $this->reformat_image_name($action, 'Thumb', $original_url);
        $thumb_path = full_static_path($thumb_url);
        $thumb_url_handle = $this->image->make_thumb($source_image, $thumb_path, $thumb_width, $thumb_height);
        if ($thumb_url_handle === FALSE) {
            $this->result['error'] = 1;
            $this->result['message'] = $this->image->error_msg();
            return $this->result;
        }
        $images['thumb'] = $thumb_url;
        $images['image'] = $image_url;
        $images['original'] = $original_url;
        return $images;
    }

    /**
     * 上传图片文件
     * @param unknown $client_id
     * @param unknown $action
     * @param unknown $image_path
     */
    public function save_temp_file($action, $original_url)
    {
        //生成缩略图，以前存放在腾讯oss中，要是生成缩略图，现在在阿里云上只要上传原图，这块隐藏
        /*try {//filename
            $images = $this->process_image_tp3($action, $original_url, 0);
            $this->db->set('module', 		$action);
            $this->db->set('thumb', 		$images['thumb']);
            $this->db->set('image',		 	$images['image']);
            $this->db->set('original', 		$original_url);
            $this->db->insert($this->table);
            $file_id = $this->db->insert_id();
            $re_data['file_id'] = $file_id;
            $re_data['format_thumb'] = get_image_path($images['thumb']);
            $re_data['format_image'] = get_image_path($images['image']);
            $re_data['format_original'] = get_image_path($original_url);
            $this->result['append_data'] = $re_data;
        } catch (Exception $e) {
            $this->result['error'] = 1;
            $this->result['message'] = $e->getMessage();
        }*/

        //上传到阿里云，只要原图就行
        try {//filename
            $this->db->set('module', $action);
//            $this->db->set('thumb', $original_url);
//            $this->db->set('image', $original_url);
            $this->db->set('original', $original_url);

            $this->db->insert($this->table);
            $file_id = $this->db->insert_id();
            $re_data['file_id'] = $file_id;
            $re_data['format_thumb'] = get_image_path($original_url);
            $re_data['format_image'] = get_image_path($original_url);
            $re_data['format_original'] = get_image_path($original_url);
            $this->result['append_data'] = $re_data;
        } catch (Exception $e) {
            $this->result['error'] = 1;
            $this->result['message'] = $e->getMessage();
        }


        return $this->result;
    }

    /**
     * 将上传文件分类整理到相应目录
     * @param unknown $item_id
     * @param unknown $image_type
     * @param unknown $source_img
     * @param string $return_path
     * @return string
     */
    function reformat_image_name($image_module, $image_type, $source_img)
    {
        $img_dir = "images/";//默认图片存储目录
        $has_date_folder = true;
        switch ($image_module) {
            case 'canteen_image':
                $has_date_folder = false;
                $img_dir .= 'canteen_image/' . $this->get_mon(2) . '/';
                break;
                default:
                $has_date_folder = false;
                $img_dir .= 'defalut/' . $this->get_mon() . '/';
                break;
        }
        if ($has_date_folder) {
            $img_dir .= sprintf("%s/", date('Y-m'));//以日期为子目录
        }
        $file_ext = pathinfo($source_img, PATHINFO_EXTENSION);

        $image_type = strtolower($image_type);
        if ($image_type == 'original') {
            $file_name = substr(md5(microtime()), 4, 16);
        } else {
            $type_arr = array('original' => 'o', 'thumb' => 't', 'image' => 'i');
            $prefix = isset($type_arr[$image_type]) ? $type_arr[$image_type] : 'o';
            $file_name = ltrim($source_img, $img_dir);
            $file_name = rtrim($file_name, '.' . $file_ext);
            $file_name .= "@" . $prefix;
        }
        $file_name .= '.' . $file_ext;
        $file_name = strtoupper($file_name);
        if(is_dir(full_static_path($img_dir))) {
            return $img_dir . $file_name;
        }
        if (make_dir(full_static_path($img_dir), true)) {
            return $img_dir . $file_name;
        } else {
            return false;
        }
    }

    //$type = 2 获取今天(20150901) 或 $type = 1 当前月(201509)
    function get_mon($type = 1)
    {
        if ($type == 1) {
            return date("Ym", time());
        } else {
            return date("Ymd", time());
        }

    }

    /**
     * 最好做成统一删除
     */
    function del_file($file_array)
    {
        if (!empty($file_array['thumb'])) {
            $format_thumb_url = full_static_path($file_array['thumb']);
            if (@is_file($format_thumb_url)) @unlink($format_thumb_url);
        }
        if (!empty($file_array['image'])) {
            $format_image_url = full_static_path($file_array['image']);
            if (@is_file($format_image_url)) @unlink($format_image_url);
        }
        if (!empty($file_array['original'])) {
            $format_original_url = full_static_path($file_array['original']);
            if (@is_file($format_original_url)) @unlink($format_original_url);
        }
    }

    //删除某张图片
    function del_img_url($img_url)
    {
        if (!empty($img_url)) {
            $format_url = full_static_path($img_url);
            if (@is_file($format_url)) @unlink($format_url);
        }
    }
}

?>